﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kojarskiy.Core;
using Kojarskiy.Models;

namespace Kojarskiy.Controllers
{
    public class GalleryController : Controller
    {
        private KojarskiyDbEntities context = new KojarskiyDbEntities();

        //
        // GET: /Gallery/

        public ActionResult Index(int? page)
        {
            var siteMap = context.SiteMap.First(x => x.Controller == "Gallery" && x.Action == "Index" && x.ParentId == 1);
            ViewData["SiteMap"] = siteMap;
            ViewBag.Title = siteMap.Title;
            ViewBag.Description = siteMap.Description;
            ViewBag.Keywords = siteMap.Keywords;

            var gallery = context.Gallery.Include(g => g.SiteMap);
            if (page != null)
            {
                gallery = context.Gallery.Where(x => x.SiteMapId == page).Include(g => g.SiteMap);
            }
            return View(gallery.ToList());
        }

        //
        // GET: /Gallery/Create

        public ActionResult Create()
        {
            ViewBag.SiteMapId = new SelectList(context.SiteMap.Where(x => x.Controller == "Gallery"), "SiteMapId", "Title");
            return View();
        }

        //
        // POST: /Gallery/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Gallery gallery, IEnumerable<HttpPostedFileBase> files)
        {
            if (ModelState.IsValid)
            {
                if (files.Any())
                {
                    foreach (var file in files)
                    {
                        if (file != null)
                        {
                            try
                            {
                                var g = new Gallery() { SiteMapId = gallery.SiteMapId, Path = "temp", ThumbPath = "temp" };
                                context.Gallery.Add(g);
                                context.SaveChanges();

                                context.Entry(g).State = EntityState.Modified;
                                string thumbPath = "";
                                string path = FileHelper.UploadPhoto(file, "Gallery", g.GalleryId, out thumbPath, true);
                                g.Path = path;
                                g.ThumbPath = thumbPath;
                                context.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                KojarskiyApplication.Log(KojarskiyApplication.ErrorType.Fatal, ex.Message, ex.StackTrace);
                            }

                        }
                    }
                    
                }
                
                return RedirectToAction("Index");
            }

            ViewBag.SiteMapId = new SelectList(context.SiteMap, "SiteMapId", "Title", gallery.SiteMapId);
            return View(gallery);
        }

        //
        // GET: /Gallery/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Gallery gallery = context.Gallery.Find(id);
            if (gallery == null)
            {
                return HttpNotFound();
            }
            return View(gallery);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Gallery gallery = context.Gallery.Find(id);

            FileHelper.DeleteFile(gallery.Path);
            FileHelper.DeleteFile(gallery.ThumbPath);

            context.Gallery.Remove(gallery);
            context.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            context.Dispose();
            base.Dispose(disposing);
        }
    }
}